package edu.uoc.tdp.pac3.client;

import javax.swing.*;
import java.rmi.RemoteException;
import edu.uoc.tdp.pac3.common.tools.*;
import edu.uoc.tdp.pac3.common.GestorIncidenciesInterface;
import java.util.Date;

/**
 * Clase Pantalla Alta nova
 * 
 * @author Eloi Rebollo i Francesc Cabrera
 * @version 1.0
 */
public class PantallaAltaNova extends JPanel {

    private GestorIncidenciesInterface objServeis = null;
    private TDSLanguageUtils mess = null;
    private JLabel lblTitle = null;
    private JLabel lblEspais = null;
    private JLabel lblUsers = null;
    private JTextArea txtArea = null;
    private JButton btnSave = null;
    private JComboBox cmbUsers = null;
    private JComboBox cmbEspais = null;

    /**
     * @param args
     */
    public static void main(String[] args) {
    }

    /**
     * This is the default constructor
     */
    public PantallaAltaNova() {
        super();
        initialize();
    }

    public PantallaAltaNova(GestorIncidenciesInterface pObjServeis) throws RemoteException {
        super();
        objServeis = pObjServeis;
        initialize();
    }

    /**
     * This method initializes this
     * 
     * @return void
     */
    private void initialize() {
        lblTitle = new JLabel();
        lblTitle.setBounds(20, 25, 300, 20);
        lblTitle.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        lblTitle.setText(mess.getMessage("altanova.title"));

        lblEspais = new JLabel();
        lblEspais.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblEspais.setBounds(20, 60, 250, 20);
        lblEspais.setText(mess.getMessage("altanova.label.espais"));

        lblUsers = new JLabel();
        lblUsers.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblUsers.setBounds(20, 90, 250, 20);
        lblUsers.setText(mess.getMessage("altanova.label.usuaris"));

        txtArea = new JTextArea();
        txtArea.setBorder(javax.swing.BorderFactory.createTitledBorder(mess.getMessage("altanova.area.title")));
        txtArea.setBounds(10, 120, 650, 190);

        this.setLayout(null);
        this.setBounds(0, 0, 800, 350);
        this.add(lblTitle, null);
        this.add(lblEspais, null);
        this.add(getCmbUser(), null);

        this.add(lblUsers, null);
        this.add(getCmbEspai(), null);
        this.add(txtArea);
        this.add(getBtnSave(), null);
    }

    /**
     * Inicialitza el contingut del combo d'espais	
     * 	
     * @return javax.swing.JComboBox	
     */
    private JComboBox getCmbEspai() {
        if (cmbEspais == null) {
            try {
                cmbEspais = new JComboBox(objServeis.vctEspais());
                cmbEspais.setBounds(300, 60, 370, 25);
            } catch (RemoteException e) {
                JOptionPane.showMessageDialog(null,
                        mess.getMessage("altanova.err.rmi") + "\n" + e.getMessage(),
                        mess.getMessage("altanova.err"), JOptionPane.ERROR_MESSAGE);
            }
        }
        return cmbEspais;
    }

    /**
     * Inicialitza el contingut del combo d'usuaris	
     * 	
     * @return javax.swing.JComboBox	
     */
    private JComboBox getCmbUser() {
        if (cmbUsers == null) {
            try {
                cmbUsers = new JComboBox(objServeis.vctUsers());
                cmbUsers.setBounds(300, 90, 370, 25);
            } catch (RemoteException e) {
                JOptionPane.showMessageDialog(null,
                        mess.getMessage("altanova.err.rmi") + "\n" + e.getMessage(),
                        mess.getMessage("altanova.err"), JOptionPane.ERROR_MESSAGE);
            }
        }
        return cmbUsers;
    }

    /**
     * Inicialitza el boto de guardar	
     * 	
     * @return javax.swing.JButton	
     */
    private JButton getBtnSave() {
        if (btnSave == null) {
            btnSave = new JButton();
            btnSave.setBounds(10, 320, 660, 30);
            btnSave.setText(mess.getMessage("altanova.button.save"));
            btnSave.addActionListener(new java.awt.event.ActionListener() {

                public void actionPerformed(java.awt.event.ActionEvent e) {                    
                    if (Guardar() == -1) {
                        JOptionPane.showMessageDialog(null,
                                mess.getMessage("altanova.action.services.ko"),
                                mess.getMessage("altanova.action.services"), JOptionPane.WARNING_MESSAGE);
                    } else {
                        JOptionPane.showMessageDialog(null,
                                mess.getMessage("altanova.action.services.ok"),
                                mess.getMessage("altanova.action.services"), JOptionPane.WARNING_MESSAGE);                        
                    }
                }
            });
        }
        return btnSave;
    }

    protected Integer Guardar() {
        
        Integer ret = -1;
        ComboItem Space = (ComboItem) cmbEspais.getSelectedItem();
        //Integer idEspai = Space.getID(); 
        ComboItem User = (ComboItem) cmbUsers.getSelectedItem();
        //Integer idUser = User.getID(); 
        //Date fecha = new Date();

        try {
            ret = objServeis.saveRequest(Space.getID(), User.getID(), new Date(), txtArea.getText());
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null,
                    mess.getMessage("altanova.action.services.ok") + "\n" + e.getMessage(),
                    mess.getMessage("altanova.action.services"), JOptionPane.ERROR_MESSAGE);
        }
        return ret;
    }
}
